package com.hotel.management.controller;

import com.hotel.management.dto.EmployeeDTO;
import com.hotel.management.dto.EmployeeQueryDTO;
import com.hotel.management.dto.PageResult;
import com.hotel.management.service.EmployeeService;
import com.hotel.management.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 员工信息管理控制器
 * 提供员工信息的RESTful API接口
 * 
 * @author Hotel Management Team
 * @version 1.0.0
 * @since 2024-06-28
 */
@RestController
@RequestMapping("/employees")
@Tag(name = "员工管理", description = "员工信息管理相关API")
public class EmployeeController {

    private static final Logger log = LoggerFactory.getLogger(EmployeeController.class);
    
    private final EmployeeService employeeService;
    
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
        log.info("EmployeeController initialized successfully with EmployeeService: {}",
                employeeService.getClass().getSimpleName());
    }

    @PostConstruct
    public void init() {
        log.info("EmployeeController @PostConstruct called - Controller is ready to handle requests");
    }

    /**
     * 创建员工
     * 
     * @param employeeDTO 员工信息
     * @return 创建的员工信息
     */
    @PostMapping
    @Operation(summary = "创建员工", description = "创建新的员工信息")
    public ResponseEntity<Result<EmployeeDTO>> createEmployee(
            @Valid @RequestBody EmployeeDTO employeeDTO) {
        log.info("创建员工请求: {}", employeeDTO.getName());
        
        EmployeeDTO createdEmployee = employeeService.createEmployee(employeeDTO);
        
        log.info("员工创建成功: ID={}, 姓名={}", createdEmployee.getId(), createdEmployee.getName());
        return ResponseEntity.status(HttpStatus.CREATED)
                .body(Result.success("员工创建成功", createdEmployee));
    }

    /**
     * 根据ID查询员工
     * 
     * @param id 员工ID
     * @return 员工信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询员工", description = "根据员工ID查询员工详细信息")
    public ResponseEntity<Result<EmployeeDTO>> getEmployeeById(
            @Parameter(description = "员工ID", required = true)
            @PathVariable @NotNull Long id) {
        log.debug("查询员工信息: ID={}", id);
        
        EmployeeDTO employee = employeeService.getEmployeeById(id);
        
        return ResponseEntity.ok(Result.success("查询成功", employee));
    }

    /**
     * 更新员工信息
     * 
     * @param id 员工ID
     * @param employeeDTO 员工信息
     * @return 更新后的员工信息
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新员工信息", description = "根据员工ID更新员工信息")
    public ResponseEntity<Result<EmployeeDTO>> updateEmployee(
            @Parameter(description = "员工ID", required = true)
            @PathVariable @NotNull Long id,
            @Valid @RequestBody EmployeeDTO employeeDTO) {
        log.info("更新员工信息: ID={}", id);
        
        EmployeeDTO updatedEmployee = employeeService.updateEmployee(id, employeeDTO);
        
        log.info("员工信息更新成功: ID={}, 姓名={}", updatedEmployee.getId(), updatedEmployee.getName());
        return ResponseEntity.ok(Result.success("员工信息更新成功", updatedEmployee));
    }

    /**
     * 删除员工
     * 
     * @param id 员工ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除员工", description = "根据员工ID删除员工信息（软删除）")
    public ResponseEntity<Result<Void>> deleteEmployee(
            @Parameter(description = "员工ID", required = true)
            @PathVariable @NotNull Long id) {
        log.info("删除员工: ID={}", id);
        
        employeeService.deleteEmployee(id);
        
        log.info("员工删除成功: ID={}", id);
        return ResponseEntity.ok(Result.success("员工删除成功", null));
    }

    /**
     * 分页查询员工列表
     * 
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页结果
     */
    @GetMapping
    @Operation(summary = "分页查询员工列表", description = "分页查询员工列表，支持基本分页参数")
    public ResponseEntity<Result<PageResult<EmployeeDTO>>> getEmployees(
            @Parameter(description = "页码，从1开始", example = "1")
            @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.debug("分页查询员工列表: pageNum={}, pageSize={}", pageNum, pageSize);
        
        EmployeeQueryDTO queryDTO = new EmployeeQueryDTO();
        queryDTO.setPageNum(pageNum);
        queryDTO.setPageSize(pageSize);
        
        PageResult<EmployeeDTO> pageResult = employeeService.getEmployeesWithPage(queryDTO);
        
        return ResponseEntity.ok(Result.success("查询成功", pageResult));
    }

    /**
     * 条件查询员工
     * 
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @GetMapping("/search")
    @Operation(summary = "条件查询员工", description = "根据条件查询员工信息，支持多种查询条件和分页")
    public ResponseEntity<Result<PageResult<EmployeeDTO>>> searchEmployees(
            @Valid EmployeeQueryDTO queryDTO) {
        log.debug("条件查询员工: {}", queryDTO);
        
        PageResult<EmployeeDTO> pageResult = employeeService.getEmployeesWithPage(queryDTO);
        
        return ResponseEntity.ok(Result.success("查询成功", pageResult));
    }

    /**
     * 批量创建员工
     * 
     * @param employeeDTOs 员工信息列表
     * @return 创建结果
     */
    @PostMapping("/batch")
    @Operation(summary = "批量创建员工", description = "批量创建多个员工信息")
    public ResponseEntity<Result<Integer>> batchCreateEmployees(
            @Valid @RequestBody @NotEmpty List<@Valid EmployeeDTO> employeeDTOs) {
        log.info("批量创建员工: 数量={}", employeeDTOs.size());
        
        int createdCount = employeeService.batchCreateEmployees(employeeDTOs);
        
        log.info("批量创建员工成功: 创建数量={}", createdCount);
        return ResponseEntity.status(HttpStatus.CREATED)
                .body(Result.success("批量创建成功", createdCount));
    }

    /**
     * 批量删除员工
     * 
     * @param ids 员工ID列表
     * @return 删除结果
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除员工", description = "批量删除多个员工信息（软删除）")
    public ResponseEntity<Result<Integer>> batchDeleteEmployees(
            @RequestBody @NotEmpty List<Long> ids) {
        log.info("批量删除员工: 数量={}", ids.size());
        
        int deletedCount = employeeService.batchDeleteEmployees(ids);
        
        log.info("批量删除员工成功: 删除数量={}", deletedCount);
        return ResponseEntity.ok(Result.success("批量删除成功", deletedCount));
    }
}
